<?php
/**
 * 优惠券操作
 * @author 李先生
 *
 */
namespace Org\Oa;
use Think\Controller;
class prefer extends Controller{
	/**
	 * 生成优惠券码
	 * $ticket_id = 优惠券的id
	 * $num 数量
	 */
	public static function add_code($ticket_id,$num){
		$ma = array();
		for($i=1;$i<=$num;$i++){
			if($ticket_id){
				$data['ticket_id'] = $ticket_id;
				$data['card_num'] =  self::parekey('prop','card_num',10);
				$data['is_send'] = 0;
				M('prop')->add($data);
				array_push($ma,$data['card_num']);
			}
			
		}
		if($ticket_id){
		   M('ticket')->where("id='{$ticket_id}'")->setInc('send_num',$num);
		   return $ma;
		}
		
	}
	/**
	 * 优惠券发送方式
	 */
	public static function coupon_type($type){
		switch ($type){
			case 1:
				$name = "按用户发放";
			break;
			case 2:
				$name = "注册发放";
			break;
			case 3:
				$name = "邀请发放";
			break;
			case 4:
				$name = "线下发放";
			break;
		}
		return $name;
	}
	/**
	 * 生成唯一值
	 */
	public static function parekey($model,$key,$length=8){
		$db = M($model);
		for($k=0;$k<=150000000;$k++){
			$uuid = \Org\Util\String::randString($length,0,'123456789');
			$re = $db->where(array($key=>$uuid))->find();
			if(empty($re)){
				break;
			}else{
			}
		}
		return $uuid;
	}
	/**
	 * 用户领取、发送优惠券
	 * $code 优惠券编码
	 * $uid 用户的id
	 */
	public static function send_user_coupon($code,$uid){
	   $data['card_num'] = $code;
	   $data['is_send'] = 0;
	   $data['is_use'] = 0;
	   $data['uid'] = 0;
	   $re = M('prop')->where($data)->order("id desc")->find();
	   if(empty($re)){
	   	  return false;
	   }else{
	   	  $today = date("Y-m-d H:i：s"); 
	   	  $ree = M('ticket')->where("id='{$re['ticket_id']}' and start_time<='{$today}' and end_time >= '{$today}' and status='0'")->find();
	   	  if(empty($ree)){
	   	  	 return false;exit;
	   	  }
	      $userDb = M('user');
	      $res = $userDb->where("id='{$uid}' and find_in_set('{$re['id']}',coupon_id)")->find();
	      if(!empty($res)){
	      	return false;
	      }else{
	      	$data['is_send'] = 1;
	      	$data['send_time'] = date("Y-m-d H:i:s");
	      	$data['uid'] = $uid;
	      	$data['id'] = $re['id'];
	      	M('prop')->save($data);
	      	$coupon_id = $userDb->where("id='{$uid}'")->getField('coupon_id');
	      	if(empty($coupon_id)){
	      		$coupon_id_string = $re['id'];
	      	}else{
	      		$coupon_id_string = $coupon_id.",".$re['id'];
	      	}
	      	$userDb->where("id='{$uid}'")->setField('coupon_id',$coupon_id_string);
	      	return $re['id'];
	      }
	   }
	}
	/**
	 * 改变优惠券的状态及删除用户表里的优惠券
	 */
	public static function update_user_coupon($preferid,$uid){
		$propDb = M('prop');
		$data['uid'] = $uid;
		$data['is_send'] = 1;
		$data['is_use'] = 0;
		$data['id'] = $preferid;
		$re = $propDb->where($data)->order("id desc")->find();
		if(!empty($re)){
			$data['is_use'] = 1;
			$data['use_time'] = date("Y-m-d H:i:s");
			$propDb->save($data);
			//删除用户的优惠券
			$userDb = M('user');
			$coupon_id = $userDb->where("id='{$uid}'")->getField('coupon_id');
			$coupon_id_array = explode(",", $coupon_id);
			foreach ($coupon_id_array as $i=>$va){
				if($va == $preferid){
					unset($coupon_id_array[$i]);
					break;
				}
			}
			if(empty($coupon_id_array)){
				$coupon_id_string = "";
			}else{
				$coupon_id_string = join(",",$coupon_id_array);
			}
			$userDb->where("id='{$uid}'")->setField('coupon_id',$coupon_id_string);
			return true;
		}else{
			return false;
		}
	}
}